package com.itheima.service.impl;

import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class CheckGroupServiceimpl implements CheckGroupService {

    @Autowired
    CheckGroupDao checkGroupDao;

    @Override
    public void addCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {

//        查询新增的检查组编码与检查组名字是否重复
        CheckGroup checkGroup1 = checkGroupDao.findByCodeAndName(checkGroup.getCode(), checkGroup.getName());
        if(checkGroup1 != null){
            throw new RuntimeException("检查组编码重复");
        }

        checkGroupDao.addCheckGroup(checkGroup);
        checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);

    }

    @Override
    public void deleteById(Integer id) {

        checkGroupDao.deleteCheckGroupAndCheckItemById(id);
        checkGroupDao.deleteCheckGroupById(id);


    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {

        List<Integer> ids = checkGroupDao.findCheckGroupIdsBySetmealId(setmealId);

        return ids;
    }

    @Override
    public List<CheckGroup> findPage(QueryPageBean queryPageBean) {

        int start = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        int end = start + queryPageBean.getPageSize();
        List<CheckGroup> checkGroups = checkGroupDao.findPage(start, end, queryPageBean.getQueryString());
        for(CheckGroup checkGroup : checkGroups){
            List<Integer> ids = checkGroupDao.findCheckItemId(checkGroup.getId());
            List<CheckItem> checkItems = new ArrayList<>();
            for(Integer id : ids){
                CheckItem checkItem = checkGroupDao.findCheckItemById(id);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
        }
        return checkGroups;
    }

    @Override
    public Long findCount() {

        return checkGroupDao.findCount() != null ? checkGroupDao.findCount() : 0L;
    }

    public void editCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {

        checkGroupDao.editCheckGroup(checkGroup);
//        删除原来的检查组和检查项的关联关系
        checkGroupDao.deleteCheckGroupAndCheckItemById(checkGroup.getId());
//        增加新的检查组和检查项
        checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);

    }

    @Override
    public List<CheckGroup> findAll() {

        List<CheckGroup> checkGroups = checkGroupDao.findAll();
        for(CheckGroup checkGroup : checkGroups){
            List<Integer> ids = checkGroupDao.findCheckItemId(checkGroup.getId());
            List<CheckItem> checkItems = new ArrayList<>();
            for(Integer id : ids){
                CheckItem checkItem = checkGroupDao.findCheckItemById(id);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
        }
        return checkGroups;

    }

    @Override
    public CheckGroup findById(Integer id) {


        CheckGroup checkGroup = checkGroupDao.findById(id);
        List<Integer> ids = checkGroupDao.findCheckItemId(id);
        List<CheckItem> checkItems = new ArrayList<>();
        for(Integer id1 : ids){
            CheckItem checkItem = checkGroupDao.findCheckItemById(id1);
            checkItems.add(checkItem);
        }
        checkGroup.setCheckItems(checkItems);
        return checkGroup;


    }

}
